/*
 * @(#) $Header$
 *
 * Copyright (C) 2013  Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.baselib.security;

import java.io.FileDescriptor;
import java.net.InetAddress;
import java.security.Permission;

/**
 * Class {@code SecurityManagerWrapper} wraps another security manager. It
 * allows one to change some policies while retaining the policies of another
 * security manager.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.baselib.security.SecurityManagerWrapper">Daniel Léonard</a>
 * @version $Revision$
 */
public abstract class SecurityManagerWrapper extends SecurityManager {

//---------------------------
// Instance variable
//---------------------------

   /** The original security manager. */
   private SecurityManager security_manager;


//---------------------------
// Constructors
//---------------------------

   /**
    * Constructor.
    * @param   security_manager   the original security manager.
    * @throws   SecurityException   if the current security manager does not
    *                               allow new security managers.
    */
   protected SecurityManagerWrapper(SecurityManager security_manager) {
      this.setSecurityManager(security_manager);
      }


//---------------------------
// Accessors and mutators
//---------------------------

   /**
    * Changes the original security manager.
    * @param   manager   the new security manager.
    */
   protected void setSecurityManager(SecurityManager manager) {
      this.security_manager = manager;
      }

   /**
    * Gets the original security manager.
    * @return   the security manager.
    */
   public SecurityManager getSecurityManager() {
      return this.security_manager;
      }


//---------------------------
// Overridden methods from java.lang.SecurityManager
//---------------------------

   /**
    * {@inheritDoc}
    */
   @Override
   public void checkPermission(Permission perm) {
      SecurityManager sm = this.getSecurityManager();
      sm.checkPermission(perm);
      }

   /**
    * {@inheritDoc}
    */
   @Override
   public void checkPermission(Permission perm, Object context) {
      SecurityManager sm = this.getSecurityManager();
      sm.checkPermission(perm, context);
      }

   /**
    * {@inheritDoc}
    */
   @Override
   public void checkCreateClassLoader() {
      SecurityManager sm = this.getSecurityManager();
      sm.checkCreateClassLoader();
      }

   /**
    * {@inheritDoc}
    */
   @Override
   public void checkAccess(Thread t) {
      SecurityManager sm = this.getSecurityManager();
      sm.checkAccess(t);
      }

   /**
    * {@inheritDoc}
    */
   @Override
   public void checkAccess(ThreadGroup g) {
      SecurityManager sm = this.getSecurityManager();
      sm.checkAccess(g);
      }

   /**
    * {@inheritDoc}
    */
   @Override
   public void checkExit(int status) {
      SecurityManager sm = this.getSecurityManager();
      sm.checkExit(status);
      }

   /**
    * {@inheritDoc}
    */
   @Override
   public void checkExec(String cmd) {
      SecurityManager sm = this.getSecurityManager();
      sm.checkExec(cmd);
      }

   /**
    * {@inheritDoc}
    */
   @Override
   public void checkLink(String lib) {
      SecurityManager sm = this.getSecurityManager();
      sm.checkLink(lib);
      }

   /**
    * {@inheritDoc}
    */
   @Override
   public void checkRead(FileDescriptor fd) {
      SecurityManager sm = this.getSecurityManager();
      sm.checkRead(fd);
      }

   /**
    * {@inheritDoc}
    */
   @Override
   public void checkRead(String file) {
      SecurityManager sm = this.getSecurityManager();
      sm.checkRead(file);
      }

   /**
    * {@inheritDoc}
    */
   @Override
   public void checkRead(String file, Object context) {
      SecurityManager sm = this.getSecurityManager();
      sm.checkRead(file, context);
      }

   /**
    * {@inheritDoc}
    */
   @Override
   public void checkWrite(FileDescriptor fd) {
      SecurityManager sm = this.getSecurityManager();
      sm.checkWrite(fd);
      }

   /**
    * {@inheritDoc}
    */
   @Override
   public void checkWrite(String file) {
      SecurityManager sm = this.getSecurityManager();
      sm.checkWrite(file);
      }

   /**
    * {@inheritDoc}
    */
   @Override
   public void checkDelete(String file) {
      SecurityManager sm = this.getSecurityManager();
      sm.checkDelete(file);
      }

   /**
    * {@inheritDoc}
    */
   @Override
   public void checkConnect(String host, int port) {
      SecurityManager sm = this.getSecurityManager();
      sm.checkConnect(host, port);
      }

   /**
    * {@inheritDoc}
    */
   @Override
   public void checkConnect(String host, int port, Object context) {
      SecurityManager sm = this.getSecurityManager();
      sm.checkConnect(host, port, context);
      }

   /**
    * {@inheritDoc}
    */
   @Override
   public void checkListen(int port) {
      SecurityManager sm = this.getSecurityManager();
      sm.checkListen(port);
      }

   /**
    * {@inheritDoc}
    */
   @Override
   public void checkAccept(String host, int port) {
      SecurityManager sm = this.getSecurityManager();
      sm.checkAccept(host, port);
      }

   /**
    * {@inheritDoc}
    */
   @Override
   public void checkMulticast(InetAddress maddr) {
      SecurityManager sm = this.getSecurityManager();
      sm.checkMulticast(maddr);
      }

   /**
    * {@inheritDoc}
    */
   @Override
   @SuppressWarnings("deprecation")
   public void checkMulticast(InetAddress maddr, byte ttl) {
      SecurityManager sm = this.getSecurityManager();
      sm.checkMulticast(maddr, ttl);
      }

   /**
    * {@inheritDoc}
    */
   @Override
   public void checkPropertiesAccess() {
      SecurityManager sm = this.getSecurityManager();
      sm.checkPropertiesAccess();
      }

   /**
    * {@inheritDoc}
    */
   @Override
   public void checkPropertyAccess(String key) {
      SecurityManager sm = this.getSecurityManager();
      sm.checkPropertyAccess(key);
      }

   /**
    * {@inheritDoc}
    */
   @Override
   public boolean checkTopLevelWindow(Object window) {
      SecurityManager sm = this.getSecurityManager();
      boolean grant = sm.checkTopLevelWindow(window);
      return grant;
      }

   /**
    * {@inheritDoc}
    */
   @Override
   public void checkPrintJobAccess() {
      SecurityManager sm = this.getSecurityManager();
      sm.checkPrintJobAccess();
      }

   /**
    * {@inheritDoc}
    */
   @Override
   public void checkSystemClipboardAccess() {
      SecurityManager sm = this.getSecurityManager();
      sm.checkSystemClipboardAccess();
      }

   /**
    * {@inheritDoc}
    */
   @Override
   public void checkAwtEventQueueAccess() {
      SecurityManager sm = this.getSecurityManager();
      sm.checkAwtEventQueueAccess();
      }

   /**
    * {@inheritDoc}
    */
   @Override
   public void checkPackageAccess(String pkg) {
      SecurityManager sm = this.getSecurityManager();
      sm.checkPackageAccess(pkg);
      }

   /**
    * {@inheritDoc}
    */
   @Override
   public void checkPackageDefinition(String pkg) {
      SecurityManager sm = this.getSecurityManager();
      sm.checkPackageDefinition(pkg);
      }

   /**
    * {@inheritDoc}
    */
   @Override
   public void checkSetFactory() {
      SecurityManager sm = this.getSecurityManager();
      sm.checkSetFactory();
      }

   /**
    * {@inheritDoc}
    */
   @Override
   public void checkMemberAccess(Class<?> clazz, int which) {
      SecurityManager sm = this.getSecurityManager();
      sm.checkMemberAccess(clazz, which);
      }

   /**
    * {@inheritDoc}
    */
   @Override
   public void checkSecurityAccess(String target) {
      SecurityManager sm = this.getSecurityManager();
      sm.checkSecurityAccess(target);
      }

   }
